package com.csjy.migration.hnnd.business;

import com.csjy.migration.hnnd.*;
import com.csjy.migration.hnnd.annotation.BusinessDataConverter;
import com.csjy.migration.hnnd.dict.Bank;

import java.sql.SQLException;
import java.util.*;

/**
 * BT_ITEM_INSTEAD_ACCOUNT_DETAIL ==> nd_responsibility_compensatory
 * 项目台账管理( 代偿记录表)
 */
@BusinessDataConverter(
        insertSql = "INSERT INTO ND_RESPONSIBILITY_COMPENSATORY(ID, CREATE_NAME, CREATE_BY, CREATE_DATE, UPDATE_NAME, UPDATE_BY, UPDATE_DATE, SYS_ORG_CODE, SYS_COMPANY_CODE, BPM_STATUS, PRO_ID, BANK, COMPENSATORY_TYPE, BANK_ACCOUNT, COMPENSATORY_MONEY, COMPENSATORY_DATE, IN_BAIL, REMARK, COMPENSATORY_INTEREST, LOAN_ID, IS_VALID, ACTUAL_COMPENSATORY_MONEY, ACTUAL_COMPENSATORY_INTEREST, ACTUAL_COMPENSATORY_DATE, PROC_ID, AMORTIZATION, ACCOUNT_NAME, BRANCH_NAME, SHARET, PRO_SOURCE) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
        updateSql = "UPDATE ND_RESPONSIBILITY_COMPENSATORY SET CREATE_NAME = ?, CREATE_BY = ?, CREATE_DATE = ?, UPDATE_NAME = ?, UPDATE_BY = ?, UPDATE_DATE = ?, SYS_ORG_CODE = ?, SYS_COMPANY_CODE = ?, BPM_STATUS = ?, PRO_ID = ?, BANK = ?, COMPENSATORY_TYPE = ?, BANK_ACCOUNT = ?, COMPENSATORY_MONEY = ?, COMPENSATORY_DATE = ?, IN_BAIL = ?, REMARK = ?, COMPENSATORY_INTEREST = ?, LOAN_ID = ?, IS_VALID = ?, ACTUAL_COMPENSATORY_MONEY = ?, ACTUAL_COMPENSATORY_INTEREST = ?, ACTUAL_COMPENSATORY_DATE = ?, PROC_ID = ?, AMORTIZATION = ?, ACCOUNT_NAME = ?, BRANCH_NAME = ?, SHARET = ?, PRO_SOURCE = ? WHERE ID = ?",
        findMigratedIdListSql = "SELECT t1.id FROM nd_responsibility_compensatory t1 LEFT JOIN ND_PROJECT_INFO  t2 ON t1.PRO_ID = t2.id WHERE t1.PRO_ID = ?",
        seq = 207
)
public class NdResponsibilityCompensatory extends AbstractBusiness {

    public NdResponsibilityCompensatory(DbUtil sourceDbUtil, DbUtil targetDbUtil, SourceBusinessData sourceData, ProgramArgument programArgument, String projectId) throws SQLException {
        super(sourceDbUtil, targetDbUtil, sourceData, programArgument, projectId);
    }

    @Override
    public void migrate() throws Exception {
        List<Map> btItemInsteadAccountDetailList = this.sourceData.getBtItemInsteadAccountDetail();
        for (Map btItemInsteadAccountDetail : btItemInsteadAccountDetailList) {
            String id = buildId(btItemInsteadAccountDetail);
            // 判断是否需要覆盖迁移
            int needMigratedResult = this.needMigrated(id);
            if(needMigratedResult == 0) {
                continue;
            }
            // 单条插入或更新
            if(needMigratedResult == 1) {
                this.targetDbUtil.updateByParams(updateSql,this.buildValues(btItemInsteadAccountDetail),true);

                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchUpdateValues.add(this.buildValues(btItemInsteadAccountDetail));
            } else {
                this.targetDbUtil.updateByParams(insertSql,this.buildValues(btItemInsteadAccountDetail));

                // 批量插入或更新 和 单条插入或更新 二选一
//                this.batchInsertValues.add(this.buildValues(btItemInsteadAccountDetail));
            }
        }

    }


    public String buildId(Map btItemInsteadAccountDetail){
        // 基础表数据业务主键
        String originalId = btItemInsteadAccountDetail.get("INSTACCNO").toString();
        return originalId;
    }


    public List buildValues(Map btItemInsteadAccountDetail) throws Exception{

        List values = new ArrayList<>();

        //ID
        values.add(buildId(btItemInsteadAccountDetail));
        //CREATE_NAME	创建人名称
        values.add(null);
        //CREATE_BY	创建人登录名称
        values.add(null);
        //CREATE_DATE	创建日期
        values.add(btItemInsteadAccountDetail.get("CREATE_TIME"));
        //UPDATE_NAME	更新人名称
        values.add(null);
        //UPDATE_BY	更新人登录名称
        values.add(null);
        //UPDATE_DATE	更新日期
        values.add(btItemInsteadAccountDetail.get("MODIFY_TIME"));
        //SYS_ORG_CODE	所属部门
        values.add(null);
        //SYS_COMPANY_CODE	所属公司
        values.add("002");
        //BPM_STATUS	流程状态
        values.add("1");
        //PRO_ID	项目ID
        values.add(this.projectId);
        //BANK	合作银行
        values.add(DictTool.getTargetDictCode(Bank.class,btItemInsteadAccountDetail.get("BANKID").toString()));
        //compensatory_type	代偿类型   新系统:0-代偿利息,2-服务性代偿 ,3-被动代偿 ,5-他方分摊 ,旧系统 2-主动代偿,3-被动代偿,5-他方分摊
        values.add(btItemInsteadAccountDetail.get("INSTACCTYPE"));
        //BANK_ACCOUNT	银行账号
        values.add(btItemInsteadAccountDetail.get("OCCURACCOUNT"));
        //COMPENSATORY_MONEY	应代偿金额
        values.add(btItemInsteadAccountDetail.get("OCCURSUM"));
        //COMPENSATORY_DATE	应代偿时间
        values.add(btItemInsteadAccountDetail.get("OCCURTIME"));
        //IN_BAIL	其中用保证金
        values.add(btItemInsteadAccountDetail.get("PROMSUM"));
        //REMARK	备注
        values.add(btItemInsteadAccountDetail.get("INSTACCMEMO") == null ? null : Tool.decodeUnicode(btItemInsteadAccountDetail.get("INSTACCMEMO").toString()));
        //COMPENSATORY_INTEREST	应代偿利息
        values.add(btItemInsteadAccountDetail.get("OCCURLIXI"));
        //LOAN_ID	放款编号
        values.add(btItemInsteadAccountDetail.get("LOANACCOUNTNO"));
        //IS_VALID	是否使用
        values.add("Y");
        //ACTUAL_COMPENSATORY_MONEY	实际代偿金额
        values.add(btItemInsteadAccountDetail.get("OCCURSUM"));
        //ACTUAL_COMPENSATORY_INTEREST	实际代偿利息
        values.add(btItemInsteadAccountDetail.get("OCCURLIXI"));
        //ACTUAL_COMPENSATORY_DATE	实际代偿时间
        values.add(btItemInsteadAccountDetail.get("OCCURTIME"));
        //PROC_ID	流程ID
        values.add(btItemInsteadAccountDetail.get("WORKFLOWNO") == null ? null : btItemInsteadAccountDetail.get("WORKFLOWNO") + "-JW");
        //TODO AMORTIZATION	他方分摊比例（他放分摊方式选择 自定义分摊)
        values.add(null);
        //ACCOUNT_NAME	开户人名
        values.add(btItemInsteadAccountDetail.get("ACCOUNTNAME"));
        //BRANCH_NAME	支行名称
        values.add(btItemInsteadAccountDetail.get("BANKBRANCH"));
        //TODO SHARET	他方分摊方式 （1：不执行2/8分摊       2：自定义分摊）
        values.add(null);
        //TODO PRO_SOURCE	项目来源
        values.add(null);


        return values;
    }

}
